package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BusinessException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.*;
import jdk.jshell.Snippet;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
    * 新增套餐
    * */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
    * 套餐分页查询
    *
    * */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealPage = new Page<>();

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        qw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,qw);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,setmealPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //获取分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealPage.setRecords(list);
        return R.success(setmealPage);
    }
    /**
    * 删除套餐
    * */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }
    /**
    * 起售/停售套餐
    * */
    @DeleteMapping("/status/{status}")
    public R<String> status(@RequestParam List<Long> ids,@PathVariable Integer status){
        //1、判断当前要删除的套餐是否处于售卖状态，如果是，不允许售卖
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        // select count(*) from setmeal where id in (1,2,3) and status = 1
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);
        int count = setmealService.count(qw);
        if (count>0){
            //不可以被删除,抛异常
            throw new BusinessException("该套餐正在售卖,不要停售");
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        setmealService.removeByIds(ids);
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<>();
        luw.in(Setmeal::getId,ids);
        luw.eq(Setmeal::getStatus,status);
        boolean setmealStatus = setmealService.update(luw);
        if (setmealStatus){
            return R.success("起售");
        }else {
            return R.error("停售");
        }
    }

    /**
    * 根据套餐id查询套餐数据,回显
    * */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
    * 修改套餐数据
    * */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }

    /**
    * 根据套餐分类id查询该套餐分类下的套餐
    * */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(qw);

        return R.success(list);
    }
}
